<?php
namespace App\Http\Controllers\Front;

use App\Service\Front\UserService;
use Illuminate\Http\Request;

class LoginController extends BaseController
{
    //用户

    //注册
    public function doRegist(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $uname = $request->post('uname', '');
        $upwd = $request->post('passwd', '');
        if (!$uname) {
            return $this->toJson(-2, '用户名必须的');
        }
        if (!preg_match('/[0-9a-zA-Z]{6,20}/', $upwd)) {
            return $this->toJson(-2, '密码格式不对');
        }
        $res = (new UserService())->doRegist($uname, $upwd);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //登录
    public function doLogin(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $uname = $request->post('uname', '');
        $upwd = $request->post('passwd', '');
        if (!$uname) {
            return $this->toJson(-2, '用户名必须的');
        }
        if (!preg_match('/[0-9a-zA-Z]{6,20}/', $upwd)) {
            return $this->toJson(-2, '密码格式不对');
        }
        $res = (new UserService())->doLogin($uname, $upwd);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}